<template>
  <div class='container'>
    <el-card class="box-card">
      <!-- 头部 -->
      <div slot="header" class="clearfix">
        <el-form :inline="true" class="demo-form-inline search">
          <el-form-item label="关键字">
            <el-input v-model="pageData.keyword" placeholder="根据编号搜索"></el-input>
          </el-form-item>
          <el-form-item class="btn">
            <el-button @click="clear">清除</el-button>
            <el-button @click="search" type="primary">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 主内容 -->
      <div>
        <el-alert
          title=""
          type="info"
          show-icon
          :closable="false">
          <template slot>
            数据一共{{totalNum}}条
          </template>
        </el-alert>
        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="id"
            label="编号"
            width="200">
          </el-table-column>
          <el-table-column
            prop="questionType"
            label="题型"
            width="90">
            <template v-slot="scope">
              {{scope.row.questionType |formatQuestionType}}
            </template>
          </el-table-column>
          <el-table-column
            prop="questionIDs"
            label="题目编号"
            width="190">
            <template v-slot="scope">
              <!-- {{ scope.row.questionIDs }} -->
              <span v-for="(item,index) in scope.row.questionIDs" :key="index" @click="onClick(item)">
                <a class="questionCode" href="javascript:void(null)">{{item.number}}</a>
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="addTime"
            label="录入时间"
            width="200">
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)"
            width="150">
          </el-table-column>
          <el-table-column
            prop="accuracyRate"
            label="正确率(%)"
            width="150">
          </el-table-column>
          <el-table-column
            prop="userName"
            label="录入人"
            width="150">
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot='scope'>
              <el-button @click="delQuestion(scope.row.id)" class="delBtn" type="danger" icon="el-icon-delete" circle></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="1"
          :page-sizes="pagesizes"
          :page-size="pageData.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="totalNum">
        </el-pagination>
      </div>
    </el-card>
    <!-- 题目预览 -->
    <QuestionsPreview :questionDetail='questionDetail' :isPreview.sync='isPreview'/>
  </div>
</template>

<script>
import { randoms, detail, removeRandoms } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview.vue'
export default {
  data () {
    return {
      pageData: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      tableData: [],
      totalNum: null,
      pagesizes: [20, 30, 40, 50],
      isPreview: false,
      questionDetail: {} // 试题解析数据（传子组件）
    }
  },
  components: {
    QuestionsPreview
  },
  created () {
    this.loadRandoms()
  },
  methods: {
    async loadRandoms () {
      const { data: res } = await randoms(this.pageData)
      // console.log(res)
      this.tableData = res.items
      this.totalNum = res.counts
    },
    handleSizeChange () {
      console.log(111)
    },
    // 进入某一页
    handleCurrentChange (val) {
      // console.log(val)
      this.pageData.page = val
      this.loadRandoms()
    },
    // 点击题号进入题目预览页面
    async onClick (data) {
      // console.log(data)
      // this.questionDetailData = {
      //   id: data.id
      // }
      this.isPreview = true
      try {
        const { data: res } = await detail({ id: data.id })
        console.log(res)
        this.questionDetail = res
      } catch (error) {
        console.log(error)
      }
    },
    // 删除问题
    delQuestion (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // console.log(id)
        await removeRandoms({ id: id })
        this.$message.success('删除成功')
        this.loadRandoms()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 搜索
    search () {
      this.loadRandoms()
    },
    // 重置
    clear () {
      this.pageData.keyword = ''
    }
  }
}
</script>

<style scoped lang='less'>
.container{
  padding: 20px;
  .search{
    display: flex;
    justify-content: space-between;
  }
  .el-form{
    width: 100%;
  }
  /deep/.el-card__header{
    padding: 20px 20px 0px!important;
    border-bottom: unset;
  }
  /deep/.el-card__body{
    padding: 0 20px 20px;
    .delBtn{
      color: #f56c6c;
      background: #fef0f0;
      border-color: #fbc4c4;
    }
    .el-alert__icon.is-big{
      font-size: 16px;
      width: 16px;
    }
    .el-alert .el-alert__description {
      margin: unset;
    }
  }
  .el-pagination{
    margin-top: 20px;
    display: flex;
    justify-content:flex-end;
    /deep/.el-pagination__jump{
      margin-left: 5px;
    }
  }
  .questionCode{
    color: #0099ff;
  }
}
</style>
